﻿using Apps.BLL.YANX;
using Apps.Common;
using Apps.Locale;
using Apps.Models;
using Apps.Web.Core;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

namespace Apps.Web.Areas.SelfRcRelated.Controllers
{
    public class SelfRcRelatedController : BaseController
    {
        YANX_RECORD_BLL bll = new YANX_RECORD_BLL();
        ValidationErrors errors = new ValidationErrors();

        [SupportFilter]
        public ActionResult Index()
        {
            return View();
        }

        

        [SupportFilter]
        public ActionResult Related(string id)
        {
            YANX_RECORD entity = bll.Info(id);
            ViewData.Model = entity;
            return View(entity);
        }

        [HttpPost]
        [SupportFilter]
        public JsonResult Related(YANX_RECORD model)
        {
            if (model != null && ModelState.IsValid)
            {
                if (string.IsNullOrEmpty(model.YAN_ID))
                {
                    return Json(JsonHandler.CreateMessage(0, "未指定编号!"));
                }
                if (string.IsNullOrEmpty(model.ITEM_NAME))
                {
                    return Json(JsonHandler.CreateMessage(0, "标题不能为空!"));
                }
                if (string.IsNullOrEmpty(model.TEST_CODE))
                {
                    return Json(JsonHandler.CreateMessage(0, "化验编码不能为空!"));
                }

                model.BATCH_CODE = bll.GetBatchCode(model.TEST_CODE);
                if (string.IsNullOrEmpty(model.BATCH_CODE))
                {
                    return Json(JsonHandler.CreateMessage(0, "化验编码输入有误，没有找到对应批次!"));
                }
                var info = bll.Info(model.YAN_ID);
                
                try
                {
                    model.ADDTIME = DateTime.Now;
                    model.ADDUSER = GetUserId();
                    bll.Update(model);
                    return Json(JsonHandler.CreateMessage(1, Resource.InsertSucceed));
                }
                catch (Exception exception)
                {
                    return Json(JsonHandler.CreateMessage(0, Resource.InsertFail)); ;
                }
            }
            else
            {
                return Json(JsonHandler.CreateMessage(0, Resource.InsertFail));
            }
        }


        #region 删除
        [HttpPost]
        [SupportFilter]
        public ActionResult Delete(string id)
        {
            var info = bll.Info(id);
            if (info.AUDIT_STATUS == "审核通过")
            {
                return Json(JsonHandler.CreateMessage(0, "已审核的数据不允许删除!"));
            }
            if (bll.Delete(id))
            {
                LogHandler.WriteServiceLog(GetUserId(), "ID:" + id, "成功", "删除", "YANX_RECORD");
                return Json(JsonHandler.CreateMessage(1, Resource.DeleteSucceed));
            }
            else
            {
                string ErrorCol = "【删除失败】";
                LogHandler.WriteServiceLog(GetUserId(), "ID" + id + "," + ErrorCol, "失败", "删除", "YANX_RECORD");
                return Json(JsonHandler.CreateMessage(0, Resource.DeleteFail + ErrorCol));
            }
        }
        #endregion

    }
}